package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysAclMapper;
import com.mmall.dao.SysRoleAclMapper;
import com.mmall.dao.SysRoleUserMapper;
import com.mmall.pojo.SysAcl;
import com.mmall.pojo.SysUser;
import com.mmall.service.ISysCoreService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Author wangtengyu
 * @Create 2018-03-16-9:14
 */
@Service("iSysCoreService")
public class SysCoreServiceImpl implements ISysCoreService {

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    @Autowired
    private SysAclMapper sysAclMapper;

    /**
     * 获得当前用户的权限点集合 用于可以不可以授权
     *
     * @return
     */
    public List<SysAcl> getCurrentUserAclList() {
        int userId = RequestHolder.getCurrentUser().getId();
        return getUserAclList(userId);
    }

    /**
     * 获取用户权限点
     *
     * @param userId
     * @return
     */
    public List<SysAcl> getUserAclList(int userId) {
        if (isSuperAdmin()) {
            return sysAclMapper.getAll();
        }
        //首先获得用户对应的角色id集合
        List<Integer> roleIdList = sysRoleUserMapper.getRoleIdListByUserId(userId);
        if (CollectionUtils.isEmpty(roleIdList)) {
            return Lists.newArrayList();
        }
        //再根据角色id集合获取权限id集合
        List<Integer> aclIdList = sysRoleAclMapper.getAclIdListByRoleId(roleIdList);
        if (CollectionUtils.isEmpty(aclIdList)) {
            return Lists.newArrayList();
        }
        return sysAclMapper.getByIdList(aclIdList);
    }


    /**
     * 超级管理员校验规则
     *
     * @return
     */
    public boolean isSuperAdmin() {
        SysUser sysUser = RequestHolder.getCurrentUser();
        if (sysUser.getMail().contains("admin")) {
            return true;
        }
        return false;
    }


    /**
     * 获得当前角色的权限点 用于回显
     *
     * @param roleId
     * @return
     */
    public List<SysAcl> getRoleAclList(int roleId) {

        List<Integer> aclIdList = sysRoleAclMapper.getAclIdListByRoleId(Lists.newArrayList(roleId));
        if (CollectionUtils.isEmpty(aclIdList)) {
            return Lists.newArrayList();
        }
        return sysAclMapper.getByIdList(aclIdList);
    }


    public boolean hasUrlAcl (String url) {
        if (isSuperAdmin()) {
            return true;
        }
        //这个url是否有对应的权限点
        List<SysAcl> urlAclList = sysAclMapper.getByUrl(url);

        if (CollectionUtils.isEmpty(urlAclList)) {
            return true;
        }

        //获得当前用户的权限集合 判断是否包括
        List<SysAcl> userAclList = getCurrentUserAclList();
        Set<Integer> userAclIdSet = Sets.newHashSet();

        //有对应的权限点,当前用户权限集合不为空才继续,否则无权限
        //为空
        if (CollectionUtils.isEmpty(userAclList)) {
            return false;
        }
        createAclSet(userAclList, userAclIdSet);

        boolean hasValidAcl=false;

        //遍历url所对应的权限点
        for (SysAcl acl : urlAclList) {

            if (acl.getStatus() == 1) {//权限点无效 判断下一个权限点
                continue;
            }
            //如果所有的权限点都无效,那应该返回true 所有的权限点都无效下面的代码不会走了
            hasValidAcl=true;
            if (userAclIdSet.contains(acl.getId())) {
                return true;
            }
        }
        if(!hasValidAcl){
            return true;
        }
        return false;

    }


    /**
     * 通过权限集合设置权限id集合
     *
     * @param aclList
     * @param aclIdSet
     */
    public void createAclSet(List<SysAcl> aclList, Set<Integer> aclIdSet) {
        for (SysAcl acl : aclList) {
            aclIdSet.add(acl.getId());
        }
    }


}
